package hdfs.test;

import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {
    FileSystem fs = null;

    @Before
    public void init() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://39.98.160.86:9000");
//        conf.set("dfs.replication", "2");
//        FileSystem fs = FileSystem.get(new URI("hdfs://39.98.160.86:9000"), conf,"root");
        fs = FileSystem.get(conf);
    }

//    @Test
//    public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
//        fs.mkdirs(new Path("/abcd"));
//    }

//    @Test
//    public void testCopyFromLocal() throws IOException, URISyntaxException, InterruptedException {
//        fs.copyFromLocalFile(new Path("/Users/heyu/Documents/JS/jsonp.html"),new Path("/abc"));
//    }

    @Test
    public void testCopyToLocal() throws IOException, URISyntaxException, InterruptedException {
        fs.copyToLocalFile(false, new Path("/test/input/test.txt"),new Path("/Users/heyu/Documents/JS"));
    }

    @Test
    public void listFile() throws IOException, URISyntaxException, InterruptedException {
        RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/hw1"), true);
        while(iter.hasNext()){
            LocatedFileStatus status = iter.next();
            System.out.println(status.getPath().getName());
        }
    }

    @After
    public void destory() throws IOException {
        fs.close();
    }
}
